package com.atguigu.tingshu.order.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/***
 * 订单支付后通知其他微服务处理后续工作的消息队列配置 -- 广播模式
 */
@Configuration
public class OrderAfterPaidMessageConfig {

    /**
     * 广播交换机
     * @return
     */
    @Bean("orderAfterPaidFanoutExchange")
    public Exchange orderAfterPaidFanoutExchange(){
        return ExchangeBuilder.fanoutExchange("order_after_paid_fanout_exchange").build();
    }

    /**
     * 专辑服务后续处理队列
     * @return
     */
    @Bean("orderAfterPaidAlbumQueue")
    public Queue orderAfterPaidAlbumQueue(){
        return QueueBuilder.durable("order_after_paid_album_queue").build();
    }

    /**
     * 专辑服务后续处理消息绑定
     * @param orderAfterPaidFanoutExchange
     * @param orderAfterPaidAlbumQueue
     * @return
     */
    @Bean
    public Binding orderAfterPaidAlbumQueueBinding(@Qualifier("orderAfterPaidFanoutExchange") Exchange orderAfterPaidFanoutExchange,
                                                   @Qualifier("orderAfterPaidAlbumQueue") Queue orderAfterPaidAlbumQueue){
        return BindingBuilder
                .bind(orderAfterPaidAlbumQueue)
                .to(orderAfterPaidFanoutExchange)
                .with("")
                .noargs();
    }

    /**
     * 搜索服务后续处理队列
     * @return
     */
    @Bean("orderAfterPaidSearchQueue")
    public Queue orderAfterPaidSearchQueue(){
        return QueueBuilder.durable("order_after_paid_search_queue").build();
    }

    /**
     * 搜索服务后续处理消息绑定
     * @param orderAfterPaidFanoutExchange
     * @param orderAfterPaidSearchQueue
     * @return
     */
    @Bean
    public Binding orderAfterPaidSearchQueueBinding(@Qualifier("orderAfterPaidFanoutExchange") Exchange orderAfterPaidFanoutExchange,
                                                   @Qualifier("orderAfterPaidSearchQueue") Queue orderAfterPaidSearchQueue){
        return BindingBuilder
                .bind(orderAfterPaidSearchQueue)
                .to(orderAfterPaidFanoutExchange)
                .with("")
                .noargs();
    }

    /**
     * 用户服务后续处理队列
     * @return
     */
    @Bean("orderAfterPaidUserQueue")
    public Queue orderAfterPaidUserQueue(){
        return QueueBuilder.durable("order_after_paid_user_queue").build();
    }

    /**
     * 用户服务后续处理消息绑定
     * @param orderAfterPaidFanoutExchange
     * @param orderAfterPaidUserQueue
     * @return
     */
    @Bean
    public Binding orderAfterPaidUserQueueBinding(@Qualifier("orderAfterPaidFanoutExchange") Exchange orderAfterPaidFanoutExchange,
                                                   @Qualifier("orderAfterPaidUserQueue") Queue orderAfterPaidUserQueue){
        return BindingBuilder
                .bind(orderAfterPaidUserQueue)
                .to(orderAfterPaidFanoutExchange)
                .with("")
                .noargs();
    }


    /**
     * 账户服务后续处理队列
     * @return
     */
    @Bean("orderAfterPaidAccountQueue")
    public Queue orderAfterPaidAccountQueue(){
        return QueueBuilder.durable("order_after_paid_account_queue").build();
    }

    /**
     * 账户服务后续处理消息绑定
     * @param orderAfterPaidFanoutExchange
     * @param orderAfterPaidAccountQueue
     * @return
     */
    @Bean
    public Binding orderAfterPaidAccountQueueBinding(@Qualifier("orderAfterPaidFanoutExchange") Exchange orderAfterPaidFanoutExchange,
                                                   @Qualifier("orderAfterPaidAccountQueue") Queue orderAfterPaidAccountQueue){
        return BindingBuilder
                .bind(orderAfterPaidAccountQueue)
                .to(orderAfterPaidFanoutExchange)
                .with("")
                .noargs();
    }



}
